﻿using System;
using System.Windows.Forms;
using Microsoft.Office.PowerPoint.STAMP.Properties;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    ///  Builds <see cref="IProgressObserver"/> instances
    /// </summary>
    public class ProgressObserverBuilder
    {
        private readonly int _captionCount;
        private readonly IWin32Window _parentForm;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProgressObserverBuilder"/> class.
        /// </summary>
        /// <param name="captionCount">The caption count.</param>
        /// <param name="parentForm">The parent form.</param>
        public ProgressObserverBuilder(int captionCount, IWin32Window parentForm)
        {
            _captionCount = captionCount;
            _parentForm = parentForm;
        }

        /// <summary>
        /// Builds the creating.
        /// </summary>
        /// <returns></returns>
        public IProgressObserver BuildCreating()
        {
            return BuildObserver(BuildCreatingObserver);
        }

        private CreationProgressObserverWithForm BuildCreatingObserver()
        {
            return new CreationProgressObserverWithForm(_parentForm, _captionCount);
        }

        /// <summary>
        /// Builds the deleting.
        /// </summary>
        /// <returns></returns>
        public IProgressObserver BuildDeleting()
        {
            return BuildObserver(BuildDeletingObserver);
        }

        private IProgressObserver BuildDeletingObserver()
        {
            return new DeletingProgressObserverWithForm(_parentForm, _captionCount);
        }

        private IProgressObserver BuildObserver(Func<IProgressObserver> buildAction)
        {
            IProgressObserver observer;
            if (_captionCount > Settings.Default.ProcessingNotificationLimit)
            {
                observer = buildAction();
            }
            else
            {
                observer = new ProgressObserverThatDoesNothing();
            }
            return observer;
        }
    }
}